﻿#ifndef ROLLINGBOX_H
#define ROLLINGBOX_H

#include <QObject>
#include <QWidget>
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>
#include <QPropertyAnimation>


class RollingBox : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int deviation READ readDeviation WRITE setDeviation)
    Q_PROPERTY(int m_value READ getValue WRITE setValue)

public:
    enum ShowDirection{
        SD_HORIZONTAL,
        SD_VERTICAL
    };
    explicit RollingBox(QWidget *parent = nullptr);

    void setRange(int min,int max);
    void setDirection(ShowDirection dir);

    int getValue(void) const { return m_value; }
    void setValue(int value);

    QColor getFrontColor(void) const { return m_frontColor; }
    void setFrontColor(const QColor color);

    int readDeviation(void);
    void setDeviation(int n);

protected:
    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void paintEvent(QPaintEvent *);

signals:
    void valueChangedSign(int value);
    void deviationChangeSign(float deviation);

private:
    void paintNum(QPainter &painter,int num,int deviation);
    void paintLine(QPainter &painter);
    void homing(void);

private:
    int m_minRange;
    int m_maxRange;
    int m_value;
    ShowDirection m_showDirection;
    QColor m_frontColor;

    bool isDragging;
    int m_deviation;
    int m_mouseSrcPos;
    int m_numSize;//计算所得的数字字符尺寸，以最长的情况为准
    QPropertyAnimation *homingAni;
};

#endif // ROLLINGBOX_H
